package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

import models.Exercicio;
import models.GrupoMuscular;


public class DAOExercicio {

	public Set<Exercicio> select() throws Exception {
		Set<Exercicio> objs = new HashSet<Exercicio>();
		try {

			String query = "SELECT e.idExercicio, e.nome, e.idGrupoMuscular, gm.nome "
					+"FROM Exercicio e, GrupoMuscular gm "
					+"WHERE gm.idGrupoMuscular = e.idGrupoMuscular";

			Statement statement = DAOConnection.getConnection().createStatement();

			ResultSet resultSet = statement.executeQuery(query);

			while(resultSet.next()) {
				Exercicio obj = new Exercicio();
				GrupoMuscular gm = new GrupoMuscular();
				obj.id = resultSet.getInt(1);
				obj.nome = resultSet.getString(2);
				obj.idGrupoMuscular = resultSet.getInt(3);

				gm.id = obj.idGrupoMuscular;
				gm.nome = resultSet.getString(4);
				
				obj.grupoMuscular = gm;
				objs.add(obj);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return objs; 
	}
	
	public Set<Exercicio> selectXNomeEGrupoMuscular(String nome) throws Exception {
		Set<Exercicio> objs = new HashSet<Exercicio>();
		nome = "%"+ nome +"%";

		try {

			String query = "SELECT e.idExercicio, e.nome, e.idGrupoMuscular, gm.nome "
					+"FROM Exercicio e, GrupoMuscular gm "
					+"WHERE gm.idGrupoMuscular = e.idGrupoMuscular"
					+"	AND (e.nome LIKE ? OR gm.nome LIKE ?)";

			PreparedStatement ps = DAOConnection.getConnection().prepareStatement(query);
			ps.setString(1, nome);
			ps.setString(2, nome);
			

			ResultSet resultSet = ps.executeQuery();

			while(resultSet.next()) {
				Exercicio obj = new Exercicio();
				GrupoMuscular gm = new GrupoMuscular();
				obj.id = resultSet.getInt(1);
				obj.nome = resultSet.getString(2);
				obj.idGrupoMuscular = resultSet.getInt(3);

				gm.id = obj.idGrupoMuscular;
				gm.nome = resultSet.getString(4);
				
				obj.grupoMuscular = gm;
				objs.add(obj);
			}

		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return objs; 
	}
	
	public Exercicio select(String nome) throws Exception {
		Exercicio obj = null;
		try	{

			String query = "SELECT idExercicio, nome, idGrupoMuscular "
					+"FROM Exercicio "
					+"WHERE nome = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(query);
			statement.setString(1, nome);
			ResultSet resultSet = statement.executeQuery();

			if (resultSet.next()) {
				obj = new Exercicio();
				obj.id = resultSet.getInt(1);
				obj.nome = resultSet.getString(2);
				obj.idGrupoMuscular = resultSet.getInt(3);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return obj; 
	}

	public Exercicio select(int id) throws Exception {
		Exercicio obj = null;
		try	{

			String query = "select idExercicio, nome, idGrupoMuscular from Exercicio where idExercicio = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(query);
			statement.setInt(1, id);
			ResultSet resultSet = statement.executeQuery();

			while(resultSet.next()) {
				obj = new Exercicio();
				obj.id = resultSet.getInt(1);
				obj.nome = resultSet.getString(2);
				obj.idGrupoMuscular = resultSet.getInt(3);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return obj; 
	}

	public void insert(Exercicio f) throws Exception {
		try {
			String insert = "insert into Exercicio(nome, idGrupoMuscular) values(?, ?)";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(insert);

			statement.setString(1, f.nome);
			statement.setInt(2, f.idGrupoMuscular);

			statement.executeUpdate();
		} catch(SQLException e) {	
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void update(Exercicio f) throws Exception {
		try {

			String update = "update Exercicio set nome = ?, idGrupoMuscular = ? where idExercicio = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(update);

			statement.setString(1, f.nome);
			statement.setInt(2, f.idGrupoMuscular);
			statement.setInt(3, f.id);


			statement.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void delete(Exercicio f) throws Exception {
		try {

			String delete = "delete from Exercicio where idExercicio = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(delete);

			statement.setInt(1, f.id);

			statement.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
	}

}
